#!/usr/bin/env expect
############################################################################
# Purpose: Test of POE functionality
#          Test of checkpoint logic (with poe)
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "11.6"
set exit_code   0
set file_in    "test$test_id.input"
set job_id     0

print_header $test_id

if { ![test_launch_poe] } {
	send_user "\nWARNING: This test is only compatible with launch/poe systems\n"
	exit $exit_code
}
if { [test_super_user] == 0 } {
	send_user "WARNING: Test can only be run as SlurmUser\n"
	exit $exit_code
}

if { [string length $partition] == 0 } {
	set partition [default_partition]
}

#
# Build input script file
#
global env
set env(CHECKPOINT) "yes"
make_bash_script $file_in "
  echo BEGIN=\$SLURM_JOB_ID
  $bin_sleep 30
  echo FINI
"

#
# Spawn the job
#
set timeout    $max_job_delay
spawn $poe ./$file_in -rmpool $partition -procs 1
set poe_id $spawn_id
expect {
	-re "BEGIN=($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: poe not responding\n"
		if {$job_id != 0}  {
			cancel_job $job_id
		}
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: SLURM_JOB_ID missing\n"
	exit 1
}

#
# Checkpoint the job
#
set ckpt_success 1
set timeout      10
spawn $scontrol checkpoint vacate $job_id
expect {
	-re "not supported" {
		set ckpt_success 0
		exp_continue
	}
	-re "error" {
		send_user "\nFAILURE: could not checkpoint the job\n"
		set ckpt_success 0
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Make sure the job does not finish if checkpoint completes
#
set timeout 40
set spawn_id $poe_id
expect {
	-re "FINI" {
		if {$ckpt_success == 1} {
			send_user "\nFAILURE: checkpoint failed\n"
			set exit_code 1
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: poe not responding\n"
		cancel_job $job_id
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	exec rm -f $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
